package Cain.fb 
{
	/**
	 * javascript api封裝 v1.02 2010.11.23
	 * @author cain in liontravel 2010
	 * @private
	 */
	public class Javascript
	{
		/**
		 * 檢查是否登入(iframe用)
		 */
		public static var iframe_login_ck:XML = new XML
		(
			<script>
				<![CDATA[
					function(_api,_id)
					{
						$ = getSwf(_id);
						var api_key = _api; //api key 
						//取得網址列後面的所有參數字串 
						var queryString = window.location.search; 
						queryString = queryString.substring(1);
						var fields = queryString.split('&'); 
						//fb_sig_added指出了user有沒有認證使用你的application  
						var fb_sig_added = 0;
						var _obData = {};
						for (var i = 0; i < fields.length; i++) 
						{
							var index = fields[i].indexOf('=');  
							var key = fields[i].substring(0, index);  
							var value = fields[i].substring(index + 1);
							//if (key.toLowerCase() == "fb_sig_added")
							if (key == "fb_sig_added")
							{  	
								fb_sig_added = value;  
							}
							_obData[key] = value;
						}
						_obData.api_key = api_key;
						$.re_iframe_login(_obData);
						/*if (fb_sig_added == 0) 
						{ 
							//如果還沒登入或還沒授權就導向  
							$.re_iframe_login("false");
						} else 
						{ 
							//如果已經ok就進行facebook javascript api初始化  
							$.re_iframe_login("true");
						}*/
						function getSwf(id) 
						{
							var isIE = navigator.appName.indexOf("Microsoft") != -1;
							return (isIE) ? window[id] : document[id];
						}
					}
					
				]]>
			</script>
		);
		/**
		 * FB初始化
		 */
		public static var fb_link:XML = new XML
		(
			<script>
				<![CDATA[
					function(_api,_id)
					{
						//$ = document.getElementById(_id);
						$ = getSwf(_id);
						FB.Event.subscribe('auth.sessionChange', reme);
						FB.init({
							apiKey: _api,
							cookie: true
						});
						FB.getLoginStatus();
						function reme(response)
						{
							$.re_data(response.status);
						}
						function getSwf(id) 
						{
							var isIE = navigator.appName.indexOf("Microsoft") != -1;
							return (isIE) ? window[id] : document[id];
						}
					}
					
				]]>
			</script>
		);
		/**
		 * 登入
		 */
		public static var login:XML = new XML
		(
			<script>
				<![CDATA[
					function (_api,_id)
					{
						//$ = document.getElementById(_id);
						$ = getSwf(_id);
						FB.init({
							apiKey: _api,
							cookie: true
						});
						FB.login(getPerms, { perms: 'publish_stream,offline_access,sms,create_event,rsvp_event,email' } )
						function getPerms(response)
						{
							$.onLogin(response.perms);
						}
						function getSwf(id) 
						{
							var isIE = navigator.appName.indexOf("Microsoft") != -1;
							return (isIE) ? window[id] : document.getElementById(id);
						}
					}
				]]>
			</script>
		);
		/**
		 * 取得session
		 */
		public static var fb_session:XML = new XML
		(
			<script>
				<![CDATA[
					function(_id)
					{
						//$ = document.getElementById(_id);
						$ = getSwf(_id);
						var session = FB.getSession();
						var _ob = { };
						if (!session) 
						{
							_ob.success = false;
						} else 
						{
							_ob.success = true;
							_ob.session_key = session.session_key;
							_ob.uid = session.uid;
							_ob.secret= session.secret;
							_ob.expires= session.expires;
							_ob.access_token= session.access_token;
							_ob.sig= session.sig;
						}
						$.onSession(_ob);
						function getSwf(id) 
						{
							var isIE = navigator.appName.indexOf("Microsoft") != -1;
							return (isIE) ? window[id] : document[id];
						}
					}
				]]>
			</script>
		);
		/**
		 * Graph API 不含post訊息
		 */
		public static var api:XML = new XML
		(
			<script>
				<![CDATA[
					function(_call,_id) {
						//$ = document.getElementById(_id);
						$ = getSwf(_id);
						FB.api(_call, function(response) 
						{
							$.response(response);
						});
						function getSwf(id) {
							var isIE = navigator.appName.indexOf("Microsoft") != -1;
							return (isIE) ? window[id] : document[id];
						}
					}
				]]>
			</script>
		);
		/**
		 * publish_stream訊息發布
		 */
		public static var publish_stream:XML = new XML
		(
			<script>
				<![CDATA[
					function(_message,_id) 
					{
						//$ = document.getElementById(_id);
						$ = getSwf(_id);
						var _ob = { };
						FB.api('/me/feed', 'post', _message, function(response) {
							if (!response || response.error) {
							_ob.success = false;
							_ob.error = response.error;
						  } else 
						  {
							_ob.success = true;
							_ob.id = response.id;
						  }
						  $.rePublish_stream(_ob);
						});
						function getSwf(id) 
						{
							var isIE = navigator.appName.indexOf("Microsoft") != -1;
							return (isIE) ? window[id] : document[id];
						}
					}
				]]>
			</script>
		); 
		/**
		 * 取消授權
		 */
		public static var disconnect:XML = new XML
		(
			<script>
				<![CDATA[
					function()
					{
						FB.api( { method: 'Auth.revokeAuthorization' } );
					}
				]]>
			</script>
		); 
		/**
		 * 按下讚
		 */
		public static var postLike:XML = new XML
		(
			<script>
				<![CDATA[
					function(_postId,_id)
					{
						function(_postId) 
						{
							$ = getSwf(_id);
							var _ob = { };
							FB.api('/'+_postId+'/likes', 'post');
						}
					}
				]]>
			</script>
		); 
		/**
		 * 刪除發文
		 */
		public static var delPost:XML = new XML
		(
			<script>
				<![CDATA[
					function(_postId,_id) 
					{
						FB.api(_postId, 'delete', function(response) {
						  if (!response || response.error) {
							alert('Error occured');
						  } else {
							alert('Post was deleted');
						  }
						});
					}
				]]>
			</script>
		); 
		
		public static var ui:XML = new XML
		(
			<script>
				<![CDATA[
					function(_call,_id) {
						$ = getSwf(_id);
						FB.ui(_call, function(response) 
						{
							$.response_ui(response);
						});
						function getSwf(id) {
							var isIE = navigator.appName.indexOf("Microsoft") != -1;
							return (isIE) ? window[id] : document[id];
						}
					}
				]]>
			</script>
		); 
	}

}